const { promisify } = require("util");

const figlet = promisify(require("figlet")); // 字符画
const clear = require("clear"); // 清屏
const chalk = require("chalk"); // 改变输出log颜色的工具

const log = (content) => console.log(chalk.green(content)); // 封装console
const clone = require('./download.js');
const spawn = async (...args) => {
  const { spawn } = require('child_process') //child_process原生自带的
  return new Promise(resolve => {
    const proc = spawn(...args) //子进程
    proc.stdout.pipe(process.stdout) //输出流插入正常流
    proc.stderr.pipe(process.stderr)//错误流
    proc.on('close', () => {
      resolve()
    })

  })
}

module.exports = async (name) => {
  clear();
  // 输出欢迎界面
  const figlet_data = await figlet("KKB");
  log(figlet_data);

  //clone
  log(`创建项目 ${name}`)
  await clone('github:44510709/wyz', name)

  //自动安装依赖
  log('安装依赖')
  await spawn('cnpm', ['install'], { cwd: `./${name}` })

  log(`
  👌
  安装完成
  ===================
  cd ${name}
  npm run serve
  ===================
    `)
  //启动
  await spawn('npm', ['run', 'serve'], { cwd: `./${name}` })
};